package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.InvoiceTax;
import com.asset.salesforce.form.StockCategoryForm;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.InvoiceTaxService;
import com.asset.salesforce.service.JoinRequestService;

@Controller
public class TaxController {
	@Autowired
	private InvoiceTaxService invoiceTaxService;
	@Autowired
	private JoinRequestService joinRequestService;
	@Autowired
	private CalendarService calendarService;
	@RequestMapping(value="/new-invoice-tax", method=RequestMethod.GET)
	public String getTax(Model model,HttpSession session){
		if (SecurityUtils.getSubject().hasRole("invoiceTaxAdd")){
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:/s/login.html";
			}
			StockCategoryForm categoryForm = new StockCategoryForm();
			model.addAttribute("categoryForm", categoryForm);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "new-invoice-tax";
		}
		else{
			return "accessDenied";
		}
	}
	@RequestMapping(value="/new-invoice-tax", method=RequestMethod.POST)
	public String addTax(HttpSession session,HttpServletRequest request,HttpServletResponse response,
			Model model, ModelMap modelMap,
			@Valid @ModelAttribute("categoryForm") StockCategoryForm categoryForm,BindingResult result){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		InvoiceTax invoiceTax = new InvoiceTax();
		Date dateCreated = new Date();
		try {
			if(categoryForm.getName() != null && !categoryForm.getName().equalsIgnoreCase("")){
				invoiceTax.setName(categoryForm.getName());
			}
			if(categoryForm.getCode() != null && !categoryForm.getCode().equalsIgnoreCase("")){
				invoiceTax.setCode(categoryForm.getCode());
			}
			if(categoryForm.getInfo() != null && !categoryForm.getInfo().equalsIgnoreCase("")){
				invoiceTax.setInfo(categoryForm.getInfo());
			}
			if(categoryForm.getDescription() != null && !categoryForm.getDescription().equalsIgnoreCase("")){
				invoiceTax.setDescription(categoryForm.getDescription());
			}
			if(categoryForm.getFeedback() != null && !categoryForm.getFeedback().equalsIgnoreCase("")){
				invoiceTax.setFeedback(categoryForm.getFeedback());
			}
			if(categoryForm.getComment() != null && !categoryForm.getComment().equalsIgnoreCase("")){
				invoiceTax.setComment(categoryForm.getComment());
			}
			if(categoryForm.getPercentage() != null && !categoryForm.getPercentage().equalsIgnoreCase("")){
				invoiceTax.setPercentage(categoryForm.getPercentage());
			}
			if(categoryForm.getValue() != null && !categoryForm.getValue().equalsIgnoreCase("")){
				invoiceTax.setValue(categoryForm.getValue());
			}
			invoiceTax.setDateCreated(dateCreated);
			invoiceTax.setDateModified(dateCreated);
			invoiceTax.setOwner(userDetails);
			
			invoiceTaxService.addTax(invoiceTax);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		model.addAttribute("categoryForm", categoryForm);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-invoice-tax";
	}
	
	
	
	
	
	
	
	
	
	
	
	

	@RequestMapping(value="/invoice-taxes" ,method=RequestMethod.GET)
	public String getTaxes(HttpSession session,Model model,HttpServletResponse response){
		if (SecurityUtils.getSubject().hasRole("viewAllTaxes")){
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:/s/login.html";
			}
				Employee userDetails = (Employee)session.getAttribute("userDetails");
				model.addAttribute("userDetails",userDetails);
				List<InvoiceTax> taxList = new ArrayList<InvoiceTax>();
				taxList = invoiceTaxService.getAllTaxes();
				model.addAttribute("taxList", taxList);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
				return "invoice-taxes";
		}
		else{
			return "accessDenied";
		}
	}
	
	
	
	
	
	
	
	

	
	@RequestMapping(value="/view-invoice-tax" ,method=RequestMethod.GET)
	public String getTax(HttpSession session,Model model,HttpServletResponse response,
			@RequestParam(value="id",required=false)Integer id){
		if (SecurityUtils.getSubject().hasRole("invoiceView")){
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:/s/login.html";
			}
				Employee userDetails = (Employee)session.getAttribute("userDetails");
				model.addAttribute("userDetails",userDetails);
				InvoiceTax tax = new InvoiceTax();
				tax = invoiceTaxService.getTaxById(id);
				model.addAttribute("tax", tax);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
				return "view-invoice-tax";
		}
		else{
			return "accessDenied";
		}
	}
	
	
	
	
	
	
	
	
	
	
}
